package cn.tedu.demo.service.impl;

import cn.tedu.demo.ex.ServiceException;
import cn.tedu.demo.mapper.UserInfoMapper;
import cn.tedu.demo.mapper.UserMapper;
import cn.tedu.demo.pojo.dto.UserLoginDTO;
import cn.tedu.demo.pojo.dto.UserRegDto;
import cn.tedu.demo.pojo.entity.User;
import cn.tedu.demo.pojo.vo.UserInfoVO;
import cn.tedu.demo.pojo.vo.UserVO;
import cn.tedu.demo.service.IUserService;
import cn.tedu.demo.service.tools.TimeSetter;
import cn.tedu.demo.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;




import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
@Service
public class UserServiceImplement implements IUserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public void addUser(UserRegDto userRegDto, HttpServletRequest request, HttpServletResponse response) {
        log.debug("开始处理【增加用户】的业务");
        log.debug("检测注册用户的手机号用来查询是否已被占用");
        String tel = userRegDto.getTel();
        UserVO userVO = userMapper.selectByTel(tel);
        if(userVO!=null){
            String message="手机号已被占用！注册失败！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        log.debug("通过检测，手机号未被占用");
        User user=new User();
        BeanUtils.copyProperties(userRegDto,user);
        user.setGmtCreate(TimeSetter.timeSetter());
        userMapper.insert(user);
        HttpSession session = request.getSession();
        session.setAttribute("user",user);
        log.debug("存入session，参数：{}",session.getAttribute("user"));
    }

    @Override
    public UserVO getUser( UserLoginDTO userLoginDTO,HttpServletRequest request, HttpServletResponse response) {
        log.debug("开始处理【查询用户】的业务");
        log.debug("检测注册用户的手机号用来查询是否存在");
        String tel = userLoginDTO.getTel();
        log.debug("手机号：{}",tel);
        UserVO userVO = userMapper.selectByTel(tel);
        if(userVO==null){
            String message="用户不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        log.debug("通过检测，账号存在");
        log.debug("检测用户密码是否正确");
        String password = userLoginDTO.getPassword();
        if(!password.equals(userVO.getPassword())){
            String message="密码错误！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_BAD_REQUEST,message);
        }
        log.debug("通过检测，密码正确");
        HttpSession session = request.getSession();
        session.setAttribute("user",userVO);
        log.debug("存入session，参数：{}",session.getAttribute("user"));
        return userVO;
    }

    @Override
    public void logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        log.debug("进入业务层，开始处理【退出登陆】的业务");
        log.debug("开始移除session中的user：{}",session.getAttribute("user"));
        session.removeAttribute("user");
    }

    @Override
    public UserInfoVO currentUser(HttpSession session) {
        log.debug("进入业务层，开始处理【获取当前用户】的业务");
        UserVO user = (UserVO) session.getAttribute("user");
        log.debug("session中的user：{}",user);
        UserInfoVO userInfoVO = userInfoMapper.getInfoByUserId(user.getId());
        log.debug("查询到的用户信息：{}",userInfoVO);

        return userInfoVO;
    }


}
